home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 2 of 3.iso / chapter7 / getind.c < prev    next >
C/C++ Source or Header  |  1996-03-06  |  8KB  |  271 lines

  1.  
  2. #include <windows.h>  
  3. #include <commctrl.h>
  4. #include "getind.h"  
  5.  
  6.  
  7. #if defined (WIN32)
  8.     #define IS_WIN32 TRUE
  9. #else
  10.     #define IS_WIN32 FALSE
  11. #endif
  12.  
  13. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  14. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  15. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  16.  
  17. HINSTANCE hInst;   // current instance
  18.  
  19. LPCTSTR lpszAppName = "App";
  20. LPCTSTR lpszTitle   = "TreeView_GetIndent()"; 
  21.  
  22.  
  23. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  24.  
  25.  
  26. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  27.                       LPTSTR lpCmdLine, int nCmdShow)
  28. {
  29.    MSG      msg;
  30.    HWND     hWnd; 
  31.    WNDCLASS wc;
  32.  
  33.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  34.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  35.    wc.cbClsExtra    = 0;                      
  36.    wc.cbWndExtra    = 0;                      
  37.    wc.hInstance     = hInstance;              
  38.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  39.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  40.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  41.    wc.lpszMenuName  = lpszAppName;              
  42.    wc.lpszClassName = lpszAppName;              
  43.  
  44.    if ( IS_WIN95 )
  45.    {
  46.       if ( !RegisterWin95( &wc ) )
  47.          return( FALSE );
  48.    }
  49.    else if ( !RegisterClass( &wc ) )
  50.       return( FALSE );
  51.  
  52.    hInst = hInstance; 
  53.  
  54.    hWnd = CreateWindow( lpszAppName, 
  55.                         lpszTitle,    
  56.                         WS_OVERLAPPEDWINDOW, 
  57.                         CW_USEDEFAULT, 0, 
  58.                         CW_USEDEFAULT, 0,  
  59.                         NULL,              
  60.                         NULL,              
  61.                         hInstance,         
  62.                         NULL               
  63.                       );
  64.  
  65.    if ( !hWnd ) 
  66.       return( FALSE );
  67.  
  68.    ShowWindow( hWnd, nCmdShow ); 
  69.    UpdateWindow( hWnd );         
  70.  
  71.    while( GetMessage( &msg, NULL, 0, 0) )   
  72.    {
  73.       TranslateMessage( &msg ); 
  74.       DispatchMessage( &msg );  
  75.    }
  76.  
  77.    return( msg.wParam ); 
  78. }
  79.  
  80.  
  81. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  82. {
  83.    WNDCLASSEX wcex;
  84.  
  85.    wcex.style         = lpwc->style;
  86.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  87.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  88.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  89.    wcex.hInstance     = lpwc->hInstance;
  90.    wcex.hIcon         = lpwc->hIcon;
  91.    wcex.hCursor       = lpwc->hCursor;
  92.    wcex.hbrBackground = lpwc->hbrBackground;
  93.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  94.    wcex.lpszClassName = lpwc->lpszClassName;
  95.  
  96.    // Added elements for Windows 95.
  97.    //...............................
  98.    wcex.cbSize = sizeof(WNDCLASSEX);
  99.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  100.                             IMAGE_ICON, 16, 16,
  101.                             LR_DEFAULTCOLOR );
  102.             
  103.    return RegisterClassEx( &wcex );
  104. }
  105.  
  106. VOID AddItemsToTree( HWND hTree );
  107.  
  108. #define IMAGESIZE 16
  109.  
  110. LPCTSTR lpszData[4] = { "Circle", "Rectangle", "Cross", "Check" };
  111.  
  112. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  113. {
  114. static HWND hTree = NULL;
  115.  
  116.    switch( uMsg )
  117.    {
  118.       case WM_CREATE  :
  119.               {
  120.                  HIMAGELIST hImage;
  121.  
  122.                  InitCommonControls();
  123.  
  124.                  hImage   = ImageList_Create( IMAGESIZE, IMAGESIZE, 
  125.                                               ILC_COLOR4 | ILC_MASK, 4, 1 ); 
  126.                  hTree = CreateWindowEx( WS_EX_CLIENTEDGE, WC_TREEVIEW, "",
  127.                                          WS_CHILD | WS_BORDER | WS_VISIBLE | 
  128.                                          WS_VSCROLL | TVS_HASLINES | 
  129.                                          TVS_HASBUTTONS | TVS_LINESATROOT |
  130.                                          TVS_DISABLEDRAGDROP,
  131.                                          10, 100, 100, 100,
  132.                                          hWnd,
  133.                                          (HMENU)1,
  134.                                          hInst,
  135.                                          NULL);
  136.  
  137.                  if ( hTree )
  138.                  {
  139.                     int i;
  140.  
  141.                     // Associate the image list with the tree view.
  142.                     //.............................................
  143.                     TreeView_SetImageList( hTree, hImage, TVSIL_NORMAL );
  144.  
  145.                     // Add the images to the tree view image list.
  146.                     //............................................
  147.                     for ( i=0; i<4; i++ )
  148.                        ImageList_AddIcon( hImage, LoadIcon( hInst, lpszData[i] ) );
  149.  
  150.                     AddItemsToTree( hTree );
  151.                  }
  152.               }
  153.               break;
  154.  
  155.       case WM_SIZE :
  156.               if ( hTree )
  157.                  MoveWindow( hTree, 0, 0, LOWORD( lParam ), HIWORD( lParam ), FALSE );
  158.               break;
  159.  
  160.       case WM_COMMAND :
  161.               switch( LOWORD( wParam ) )
  162.               {
  163.                  case IDM_INCINDENT :
  164.                  case IDM_DECINDENT :
  165.                         {
  166.                            int nIndent;
  167.  
  168.                            // Get the first item in the tree.
  169.                            //................................
  170.                            nIndent = TreeView_GetIndent( hTree );
  171.  
  172.                            if ( LOWORD( wParam ) == IDM_INCINDENT )
  173.                               nIndent++;
  174.                            else
  175.                               nIndent--;
  176.  
  177.                            TreeView_SetIndent( hTree, nIndent );
  178.                         }
  179.                         break;
  180.  
  181.                  case IDM_ABOUT :
  182.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  183.                         break;
  184.  
  185.                  case IDM_EXIT :
  186.                         DestroyWindow( hWnd );
  187.                         break;
  188.               }
  189.               break;
  190.       
  191.       case WM_DESTROY :
  192.               if ( TreeView_GetImageList( hTree, TVSIL_NORMAL ) )
  193.                  ImageList_Destroy( TreeView_GetImageList( hTree, TVSIL_NORMAL ) );
  194.  
  195.               PostQuitMessage(0);
  196.               break;
  197.  
  198.       default :
  199.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  200.    }
  201.  
  202.    return( 0L );
  203. }
  204.  
  205.  
  206. VOID AddItemsToTree( HWND hTree )
  207. {
  208.    int             i, j;
  209.    char            szTmp[40];
  210.    TV_INSERTSTRUCT tv;
  211.    HTREEITEM       hParent;
  212.  
  213.    tv.hInsertAfter = TVI_LAST;
  214.    tv.item.mask    = TVIF_TEXT | TVIF_IMAGE | 
  215.                      TVIF_SELECTEDIMAGE | TVIF_PARAM;
  216.  
  217.    // Add the items to the tree view.
  218.    //................................
  219.    for ( i=0; i<20; i++ )
  220.    {
  221.       wsprintf( szTmp, "%s %d", lpszData[0], i+1 );
  222.  
  223.       tv.hParent      = TVI_ROOT;
  224.       tv.item.pszText = szTmp;
  225.       tv.item.iImage  = 0;
  226.       tv.item.iSelectedImage = 0;
  227.       tv.item.lParam         = 0;
  228.  
  229.       hParent = TreeView_InsertItem( hTree, &tv );
  230.       tv.hParent = hParent;
  231.  
  232.       // Add child items.
  233.       //.................
  234.       for( j=1; j<4; j++ )
  235.       {
  236.          tv.item.pszText = (LPTSTR)lpszData[j];
  237.          tv.item.iImage  = j;
  238.          tv.item.iSelectedImage = j;
  239.          tv.item.lParam         = 1;
  240.  
  241.          TreeView_InsertItem( hTree, &tv );
  242.       }
  243.    }
  244. }
  245.  
  246.  
  247. LRESULT CALLBACK About( HWND hDlg,           
  248.                         UINT message,        
  249.                         WPARAM wParam,       
  250.                         LPARAM lParam)
  251. {
  252.    switch (message) 
  253.    {
  254.        case WM_INITDIALOG: 
  255.                return (TRUE);
  256.  
  257.        case WM_COMMAND:                              
  258.                if (   LOWORD(wParam) == IDOK         
  259.                    || LOWORD(wParam) == IDCANCEL)    
  260.                {
  261.                        EndDialog(hDlg, TRUE);        
  262.                        return (TRUE);
  263.                }
  264.                break;
  265.    }
  266.  
  267.    return (FALSE); 
  268. }
  269.  
  270.  
  271.